<?php

namespace app\admin\controller;

use app\admin\model\Admin;
use app\admin\model\User;
use app\common\controller\Backend;
use app\common\model\Attachment;
use fast\Date;
use think\Db;

/**
 * 控制台
 *
 * @icon   fa fa-dashboard
 * @remark 用于展示当前系统中的统计数据、统计报表及重要实时数据
 */
class Dashboard extends Backend
{

    /**
     * 工作台展示
     */
    public function index()
    {
        $dbTableList = Db::query("SHOW TABLE STATUS");
        //待审核的商家认证数
        $user_authencount=db('user')->where(['authen'=>'2'])->count();
        //待审核申请注销用户数
        $user_logcount=db('user')->where(['logout'=>'1'])->count();
        //待审核的帖子数
        $circle_authencount=db('user_circle')->where(['authen'=>'1'])->count();
        //待审核的商家保证金退款数
        $deposit_authencount=db('user_deposit')->where(['authen_tui'=>'1','type'=>'2'])->count();
        //未处理的举报数
        $report_count=db('report')->where(['status'=>'0'])->count();
        $this->view->assign([
            'dbsize'            => array_sum(array_map(function ($item) {
                return $item['Data_length'] + $item['Index_length'];
            }, $dbTableList)),
            'attachmentsize'    => Attachment::sum('filesize'),
            'picturenums'       => Attachment::where('mimetype', 'like', 'image/%')->count(),
            'picturesize'       => Attachment::where('mimetype', 'like', 'image/%')->sum('filesize'),
            'user_authencount' => $user_authencount,
            'user_logcount' =>$user_logcount,
            'circle_authencount' =>$circle_authencount,
            'deposit_authencount' =>$deposit_authencount,
            'report_count' =>$report_count
        ]);
        return $this->view->fetch();
    }

    /**
     * 数据统计
     */
    public function curve()
    {
        try {
            \think\Db::execute("SET @@sql_mode='';");
        } catch (\Exception $e) {

        }
        $column = [];
        $starttime = Date::unixtime('day', -6);
        $endtime = Date::unixtime('day', 0, 'end');
        //用户
        $joinlist = Db("user")->where('add_time', 'between time', [$starttime, $endtime])
            ->field('add_time, COUNT(*) AS nums, DATE_FORMAT(FROM_UNIXTIME(add_time), "%Y-%m-%d") AS join_date')
            ->group('join_date')
            ->select();
        for ($time = $starttime; $time <= $endtime;) {
            $column[] = date("Y-m-d", $time);
            $time += 86400;
        }
        $userlist = array_fill_keys($column, 0);
        foreach ($joinlist as $k => $v) {
            $userlist[$v['join_date']] = $v['nums'];
        }
        // 帖子数据
        $circle_list_data = Db("user_circle")->where('add_time', 'between time', [$starttime, $endtime])
            ->field('add_time, COUNT(*) AS nums, DATE_FORMAT(FROM_UNIXTIME(add_time), "%Y-%m-%d") AS join_date')
            ->group('join_date')
            ->select();
        $circle_list = array_fill_keys($column, 0);
        foreach ($circle_list_data as $k => $v) {
            $circle_list[$v['join_date']] = $v['nums'];
        }

        // 发帖、置顶收费
        $circle_price_list_data = Db("user_circle_price")->where('add_time', 'between time', [$starttime, $endtime])
            ->where(['ispay'=>'1','istui'=>'0'])
            ->field('add_time, SUM(price) AS nums, DATE_FORMAT(FROM_UNIXTIME(add_time), "%Y-%m-%d") AS join_date')
            ->group('join_date')
            ->select();
        $circle_price_list = array_fill_keys($column, 0);
        foreach ($circle_price_list_data as $k => $v) {
            $circle_price_list[$v['join_date']] = $v['nums'];
        }

        // 商家认证费用
        $certification_list_data = Db("user_certification")->where('add_time', 'between time', [$starttime, $endtime])
            ->where(['ispay'=>'1','istui'=>'0'])
            ->field('add_time, SUM(price) AS nums, DATE_FORMAT(FROM_UNIXTIME(add_time), "%Y-%m-%d") AS join_date')
            ->group('join_date')
            ->select();
        $certification_list = array_fill_keys($column, 0);
        foreach ($certification_list_data as $k => $v) {
            $certification_list[$v['join_date']] = $v['nums'];
        }

        // 平台扣的保证金金额
        $group_user_list_data = Db("chat_group_user")->where('add_time', 'between time', [$starttime, $endtime])
            ->where(['ispay'=>'1','is_tui'=>'1'])
            ->field('add_time, SUM(kou_price) AS nums, DATE_FORMAT(FROM_UNIXTIME(add_time), "%Y-%m-%d") AS join_date')
            ->group('join_date')
            ->select();
        $group_user_list = array_fill_keys($column, 0);
        foreach ($group_user_list_data as $k => $v) {
            $group_user_list[$v['join_date']] = $v['nums'];
        }

        $dbTableList = Db::query("SHOW TABLE STATUS");
        $addonList = get_addon_list();
        $totalworkingaddon = 0;
        $totaladdon = count($addonList);
        foreach ($addonList as $index => $item) {
            if ($item['state']) {
                $totalworkingaddon += 1;
            }
        }
        //查询帖子总数，
        $circlecount=db('user_circle')->count();
        $this->view->assign([
            'totaluser'         => User::count(),
            'totaladdon'        => $totaladdon,
            'totaladmin'        => Admin::count(),
            'totalcategory'     => \app\common\model\Category::count(),
            'todayusersignup'   => User::whereTime('add_time', 'today')->count(),
            'todayuserlogin'    => User::whereTime('logintime', 'today')->count(),
            'sevendau'          => User::whereTime('add_time|logintime', '-7 days')->count(),
            'thirtydau'         => User::whereTime('add_time|logintime', '-30 days')->count(),
            'threednu'          => User::whereTime('add_time', '-3 days')->count(),
            'sevendnu'          => User::whereTime('add_time', '-7 days')->count(),
            'dbtablenums'       => count($dbTableList),
            'dbsize'            => array_sum(array_map(function ($item) {
                return $item['Data_length'] + $item['Index_length'];
            }, $dbTableList)),
            'totalworkingaddon' => $totalworkingaddon,
            'attachmentnums'    => Attachment::count(),
            'attachmentsize'    => Attachment::sum('filesize'),
            'picturenums'       => Attachment::where('mimetype', 'like', 'image/%')->count(),
            'picturesize'       => Attachment::where('mimetype', 'like', 'image/%')->sum('filesize'),
            'circlecount' => $circlecount,
        ]);
        $this->assignconfig('column', array_keys($userlist));
        $this->assignconfig('userdata', array_values($userlist));
        $this->assignconfig('circle_data', array_values($circle_list));
        $this->assignconfig('price_data', array_values($circle_price_list));
        $this->assignconfig('certification_data', array_values($certification_list));
        $this->assignconfig('group_user_data', array_values($group_user_list));
        $this->assignconfig('start_date',date('Y-m-d',$starttime));
        $this->assignconfig('end_date',date('Y-m-d',$endtime));
        return $this->view->fetch();
    }

    public function curveList()
    {
        $start_time = input("start_time");
        $end_time = input("end_time");
//        $start_time = "2024-07-31";
//        $end_time = "2024-08-06";
        $starttime = strtotime($start_time);
        $endtime = strtotime($end_time);
        $column = [];
        //用户
        $joinlist = Db("user")->where('add_time', 'between time', [$starttime, $endtime])
            ->field('add_time, COUNT(*) AS nums, DATE_FORMAT(FROM_UNIXTIME(add_time), "%Y-%m-%d") AS join_date')
            ->group('join_date')
            ->select();
        for ($time = $starttime; $time <= $endtime;) {
            $column[] = date("Y-m-d", $time);
            $time += 86400;
        }
        $userlist = array_fill_keys($column, 0);
        foreach ($joinlist as $k => $v) {
            $userlist[$v['join_date']] = $v['nums'];
        }
        // 帖子数据
        $circle_list_data = Db("user_circle")->where('add_time', 'between time', [$starttime, $endtime])
            ->field('add_time, COUNT(*) AS nums, DATE_FORMAT(FROM_UNIXTIME(add_time), "%Y-%m-%d") AS join_date')
            ->group('join_date')
            ->select();
        $circle_list = array_fill_keys($column, 0);
        foreach ($circle_list_data as $k => $v) {
            $circle_list[$v['join_date']] = $v['nums'];
        }

        // 发帖、置顶收费
        $circle_price_list_data = Db("user_circle_price")->where('add_time', 'between time', [$starttime, $endtime])
            ->where(['ispay'=>'1','istui'=>'0'])
            ->field('add_time, SUM(price) AS nums, DATE_FORMAT(FROM_UNIXTIME(add_time), "%Y-%m-%d") AS join_date')
            ->group('join_date')
            ->select();
        $circle_price_list = array_fill_keys($column, 0);
        foreach ($circle_price_list_data as $k => $v) {
            $circle_price_list[$v['join_date']] = $v['nums'];
        }

        // 商家认证费用
        $certification_list_data = Db("user_certification")->where('add_time', 'between time', [$starttime, $endtime])
            ->where(['ispay'=>'1','istui'=>'0'])
            ->field('add_time, SUM(price) AS nums, DATE_FORMAT(FROM_UNIXTIME(add_time), "%Y-%m-%d") AS join_date')
            ->group('join_date')
            ->select();
        $certification_list = array_fill_keys($column, 0);
        foreach ($certification_list_data as $k => $v) {
            $certification_list[$v['join_date']] = $v['nums'];
        }

        // 平台扣的保证金金额
        $group_user_list_data = Db("chat_group_user")->where('add_time', 'between time', [$starttime, $endtime])
            ->where(['ispay'=>'1','is_tui'=>'1'])
            ->field('add_time, SUM(kou_price) AS nums, DATE_FORMAT(FROM_UNIXTIME(add_time), "%Y-%m-%d") AS join_date')
            ->group('join_date')
            ->select();
        $group_user_list = array_fill_keys($column, 0);
        foreach ($group_user_list_data as $k => $v) {
            $group_user_list[$v['join_date']] = $v['nums'];
        }
        $data['column'] = array_keys($userlist);
        $data['userdata'] = array_values($userlist);
        $data['circle_data'] = array_values($circle_list);
        $data['price_data'] = array_values($circle_price_list);
        $data['certification_data'] = array_values($certification_list);
        $data['group_user_data'] = array_values($group_user_list);
        return json($data);
    }

}
